package com.BraveY.Demo05;

import java.util.ArrayList;
import java.util.Scanner;

public class Deme01 {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();

        lo:
        while (true) {
            System.out.println("----------欢迎使用学生管理系统！--------------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String select = S.next();

            switch (select) {
                case "1":
//                    System.out.println("添加学生");
                    addList(list);
                    break;
                case "2":
//                    System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
//                    System.out.println("修改学生");
                    setList(list);
                    break;
                case "4":
//                    System.out.println("查看学生");
                    showList(list);
                    break;
                case "5":
                    System.out.println("感谢使用此系统！再见！");
                    break lo;
            }
        }

    }

    private static void setList(ArrayList<Student> list) {
        System.out.println("请输入您要修改学生的学号");
        Scanner Sc = new Scanner(System.in);
        int updateId = Sc.nextInt();
        int index = getIndex(list, updateId);
        if (index == -1) {
            System.out.println("您要修改的学号不存在！");
        } else {
            System.out.println("请输入新的学生姓名");
            String name = Sc.next();
            System.out.println("请输入新的学生年龄");
            int age = Sc.nextInt();
            System.out.println("请输入新的学生性别");
            String sex = Sc.next();
            Student stu = new Student(updateId, name, age, sex);
            list.set(index, stu);
            System.out.println("修改成功！！");
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        System.out.println("请输入您要删除学生的学号");
        Scanner Sc = new Scanner(System.in);
        int deleteId = Sc.nextInt();
        int index = getIndex(list, deleteId);
        if (index == -1) {
            System.out.println("您要删除的学号不存在！");
        } else {
            list.remove(index);
            System.out.println("删除成功！！");
        }

    }

    public static void showList(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("系统内暂无学生录入，请录入学生！");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println("学号：" + stu.getId() + "\t\t" + "姓名：" + stu.getName() + "\t\t" + "年龄：" + stu.getAge() + "\t\t" + "性别：" + stu.getSex());
        }
    }

    public static void addList(ArrayList<Student> list) {
        Scanner Sc = new Scanner(System.in);
        int id;
        while (true) {
            System.out.println("请输入学号：");
            id = Sc.nextInt();
            int index = getIndex(list, id);
            if (index == -1) break;
        }
        System.out.println("请输入姓名：");
        String name = Sc.next();
        System.out.println("请输入年龄：");
        int age = Sc.nextInt();
        System.out.println("请输入性别：");
        String sex = Sc.next();
        Student stu = new Student(id, name, age, sex);
        list.add(stu);
        System.out.println("添加学生成功！");
    }

    public static int getIndex(ArrayList<Student> list, int sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            int id = stu.getId();
            if (id == sid) {
                index = i;
            }
        }
        return index;
    }

}
